package com.lly.songhigh.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lly.songhigh.R;
import com.lly.songhigh.activity.SongSheetDetailActivity;
import com.lly.songhigh.adapter.SongSheetGridViewAdapter;
import com.lly.songhigh.entity.SongSheet;
import com.lly.songhigh.view.SwipeRefreshLoadingLayout;
import com.lly.songhigh.volley.StringRequestUtf;
import com.lly.songhigh.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/23.
 */
public class SongSheetClassifyFragment extends BaseFragment implements SwipeRefreshLoadingLayout.OnRefreshListener, SwipeRefreshLoadingLayout.OnLoadListener, AdapterView.OnItemClickListener {

    private GridView songSheetHotGridview;
    private SwipeRefreshLoadingLayout songSheetHotLoadingLayout;
    private VolleySingleton volleySingleton;
    private int page = 1;
    private List<SongSheet.DataEntity> datas;
    private SongSheetGridViewAdapter adapter;
    private String startUrl;
    private String endUrl;

    // 通过静态方法在创建Fragment的时候传入不同的参数
    public static BaseFragment newInstance(String startUrl, String endUrl){
        BaseFragment f = new SongSheetClassifyFragment();
        Bundle arg = new Bundle();
        arg.putString("startUrl", startUrl);
        arg.putString("endUrl", endUrl);
        f.setArguments(arg);
        return f;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_song_sheet_hot, null);
        initView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance();
        startUrl = getArguments().getString("startUrl");
        endUrl = getArguments().getString("endUrl");
        // 获取屏幕宽高等信息
        DisplayMetrics metric = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;     // 屏幕宽度（像素）
        songSheetHotGridview.setColumnWidth(width/2);
        songSheetHotGridview.setMinimumHeight(width/2);
        initData();
    }

    private void initView(View view) {
        songSheetHotGridview = (GridView) view.findViewById(R.id.song_sheet_hot_gridview);
        songSheetHotLoadingLayout = (SwipeRefreshLoadingLayout) view.findViewById(R.id.song_sheet_hot_loading_layout);
        songSheetHotGridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
        songSheetHotLoadingLayout.setOnRefreshListener(this);
        songSheetHotLoadingLayout.setOnLoadListener(this);
        songSheetHotLoadingLayout.setTopColor(R.color.transparentColor, R.color.transparentColor, R.color.transparentColor, R.color.transparentColor);
        songSheetHotLoadingLayout.setBottomColor(R.color.themeColor, R.color.whiteColor, R.color.themeColor, R.color.whiteColor);
        songSheetHotGridview.setOnItemClickListener(this);
    }

    private void initData() {
        StringRequestUtf stringRequestUtf = new StringRequestUtf(startUrl + 1 + endUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        SongSheet songSheet = gson.fromJson(response, SongSheet.class);
                        datas = songSheet.getData();
                        adapter = new SongSheetGridViewAdapter(getActivity(), datas);
                        songSheetHotGridview.setAdapter(adapter);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(stringRequestUtf);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onRefresh() {
        songSheetHotLoadingLayout.setRefreshing(false);
    }

    // 上拉加载
    @Override
    public void onLoad() {
        page++;
        String url = startUrl + page + endUrl;
        StringRequestUtf stringRequestUtf = new StringRequestUtf(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        songSheetHotLoadingLayout.setLoading(false);
                        Gson gson = new Gson();
                        SongSheet songSheet = gson.fromJson(response, SongSheet.class);
                        datas = songSheet.getData();
                        adapter.addDatas(datas);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(stringRequestUtf);
    }

    // 点击行布局跳到歌单详情页
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SongSheet.DataEntity dataEntity = (SongSheet.DataEntity) parent.getItemAtPosition(position);
        int _id = dataEntity.get_id();
        Intent intent = new Intent(getActivity(), SongSheetDetailActivity.class);
        intent.putExtra("id", _id);
        startActivity(intent);
    }
}
